﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Antu.WebControls;

namespace WebSys.UI.Control.ZhengShou
{
    public partial class getajaxControl : MPBasicTemplate
    {
        protected override void OnLoad(EventArgs e)
        {
            if (Page.Request["act"] != null && Page.Request["act"] == "login")
            {
                base.OnLoad(e);
                Page.Response.Clear();
                string username = "";
                string returnstr = "";

                //从数据库中根据sn获取用户名
                AccessDbClass mydb = new AccessDbClass();
                DataTable dt = new DataTable();
                string sn = Page.Request["sn"];
                dt = mydb.SelectToDataTable(@"select * from zs_user_my where sn='" + Page.Request["sn"] + "'");
                if (dt.Rows.Count > 0)
                {
                    username = dt.Rows[0]["username"].ToString();
                }
                mydb.Close();

                AccessDbClass mydb1 = new AccessDbClass();
                DataTable dt1 = new DataTable();
                dt1 = mydb1.SelectToDataTable(@"select * from zs_user where loginid='" + username + "'");
                if (dt1.Rows.Count > 0)
                {
                    username = dt.Rows[0]["username"].ToString();
                }
               
                mydb1.Close();

                returnstr = username + "|" + GetMD516SN(username);
                Page.Response.Write(returnstr);
                Page.Response.End();
            }
            if (Page.Request["act"] != null && Page.Request["act"] == "updatepwd")
            {
                Page.Response.Clear();
                string returnstr = "";

                //数据库中更改pwd;
                AccessDbClass mydb = new AccessDbClass();
                DataTable dt = new DataTable();
                string username = string.Empty;
                dt = mydb.SelectToDataTable(@"select * from zs_user_my where sn='" + Page.Request["sn"] + "'");
                if (dt.Rows.Count > 0)
                {
                    username = dt.Rows[0]["username"].ToString();
                }
                mydb.Close();

                AccessDbClass mydb1 = new AccessDbClass();
                if (!mydb1.ExecuteSQLNonquery(@"update zs_user_my set pwd='" + Page.Request["pwd"] + "' where sn='" + Page.Request["sn"] + "'"))
                {
                    Page.Response.Write("密码修改失败！");
                    Page.Response.End();
                    return;
                }

                mydb1.Close();
                AccessDbClass mydb2 = new AccessDbClass();

                if (!mydb2.ExecuteSQLNonquery(@"update zs_user set pwd='" + Page.Request["pwd"] + "' where loginid='" + username + "'"))
                {
                    Page.Response.Write("密码修改失败！");
                    Page.Response.End();
                    return;
                }

                mydb2.Close();
                Page.Response.Write("ok");
                Page.Response.End();
            }
            if (Page.Request["act"] != null && Page.Request["act"] == "clearsession")
            {
                Page.Response.Clear();
                Page.Session["UserName"] = null;
                Page.Response.Write("ok");
                Page.Response.End();
            }
        }
        public string GetMD516SN(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string snnamepin = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
            return snnamepin;
        }
    }
}
